package liwen.zhao.utils;

import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("esUtil")
public class ESUtil {
    @Autowired
    TransportClient transportClient;
    
    //创建索引
    public TransportClient  indexIsExists(String name){
        transportClient.admin().indices().prepareCreate(name).get().isAcknowledged();
        //拿到集群索引管理对象
        IndicesAdminClient indicesAdminClient = transportClient.admin().indices();
        //判断这个索引是否存在
        IndicesExistsResponse indicesExistsResponse = indicesAdminClient.prepareExists(name).get();
        if(!indicesExistsResponse.isExists()){
            throw  new RuntimeException("already exists");
        }else{
            return  transportClient;
        }
    }
    
}
